' this procedure checks the user name against a database of users
' (it is provided as an example, as no user database is provided
' with this demo)
' replace this with a real connection string
Dim PasswordDBConnString As String = "... a connection string ..."
Function AuthenticateUser(ByVal username As String, ByVal password As String) As Boolean
' Open the connection to the database holding user names and passwords.
Dim cn As New OleDbConnection(PasswordDBConnString)
cn.Open()
' Read the record for this user.
Dim cmd As New OleDbCommand("SELECT * FROM Users WHERE UserName=?", cn)
cmd.Parameters.Add("username", username)
Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
If dr.Read AndAlso dr("Password") = password Then
' Authenticate user if there is a record and the password is correct.
AuthenticateUser = True
End If
' Close the DataReader (and the connection).
dr.Close()
End Function
' A custom routine that works like FormsAuthentication.RedirectFromLoginPage
' but lets you control the authentication cookie's expiration date.
Function RedirectFromLoginPageEx(ByVal username As String, ByVal persistentCookie As Boolean, Optional ByVal expirationDays As Integer = -1) As Boolean
' Get the URL of the requested resource.
Dim url As String = FormsAuthentication.GetRedirectUrl(username, persistentCookie)
' Create the authentication cookie.
' (The cookie path can be omitted, because it defaults to "/", the entire site.)